package com.jeequan.jeepay.mgr.controller;

//package .controller;

import com.jeequan.jeepay.core.entity.PaymentOrder;
import com.jeequan.jeepay.mgr.ctrl.CommonCtrl;
import com.jeequan.jeepay.service.PaymentOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
//import .entity.PaymentOrder;
//import .service.PaymentOrderService;

/**
* 代付订单表;(payment_order)表控制层
* @author : wondream322@gmail.com
* @date : 2023-8-19
*/
@Api(tags = "代付订单表对象功能接口")
@RestController
@RequestMapping("/api/paymentOrder")
public class PaymentOrderController extends CommonCtrl {
   @Autowired
   private PaymentOrderService paymentOrderService;

   /**
    * 通过ID查询单条数据
    *
    * @param id 主键
    * @return 实例对象
    */
   @ApiOperation("通过ID查询单条数据")
   @GetMapping("{id}")
   public ResponseEntity<PaymentOrder> queryById(@PathVariable("id") Integer id){
       return ResponseEntity.ok(paymentOrderService.queryById(id));
   }

   /**
    * 分页查询
    *
    * @param paymentOrder 筛选条件
    * @return 查询结果
    */
   @ApiOperation("分页查询")
   @PostMapping("/paginQuery")
   public ResponseEntity<PageImpl<PaymentOrder>> paginQuery(@RequestBody PaymentOrder paymentOrder){
       PageRequest pageRequest = PageRequest.of(com.jeequan.jeepay.core.constants.CS.DEFALT_PAGE_NUM, com.jeequan.jeepay.core.constants.CS.DEFALT_PAGE_SIZE);
       //1.分页参数
       long current = pageRequest.getPageNumber();
       long size = pageRequest.getPageSize();
       //2.分页查询
       /*把Mybatis的分页对象做封装转换，MP的分页对象上有一些SQL敏感信息，还是通过spring的分页模型来封装数据吧*/
       com.baomidou.mybatisplus.extension.plugins.pagination.Page<PaymentOrder> pageResult = paymentOrderService.paginQuery(paymentOrder, current,size);
       //3. 分页结果组装
       List<PaymentOrder> dataList = pageResult.getRecords();
       long total = pageResult.getTotal();
       PageImpl<PaymentOrder> retPage = new PageImpl<PaymentOrder>(dataList,pageRequest,total);
       return ResponseEntity.ok(retPage);
   }

   /**
    * 新增数据
    *
    * @param paymentOrder 实例对象
    * @return 实例对象
    */
   @ApiOperation("新增数据")
   @PostMapping("/add")
   public ResponseEntity<PaymentOrder> add(@RequestBody PaymentOrder paymentOrder){
       return ResponseEntity.ok(paymentOrderService.insert(paymentOrder));
   }

   /**
    * 更新数据
    *
    * @param paymentOrder 实例对象
    * @return 实例对象
    */
   @ApiOperation("更新数据")
   @PostMapping("/edit")
   public ResponseEntity<PaymentOrder> edit(@RequestBody PaymentOrder paymentOrder){
       return ResponseEntity.ok(paymentOrderService.update(paymentOrder));
   }

   /**
    * 通过主键删除数据
    *
    * @param id 主键
    * @return 是否成功
    */
   @ApiOperation("通过主键删除数据")
   @GetMapping("/delete")
   public ResponseEntity<Boolean> deleteById(@PathVariable("id") Integer id){
       return ResponseEntity.ok(paymentOrderService.deleteById(id));
   }
}